const {execSql} = require("../utils/db")
class cart{
    /**
     * 
     * @param {number/string} uid =>用户id 或者temp_key  
     */
    static async get(uid){
        //如果商品下架了 购物车数据 要置灰 不允许操作  购物车购买 数量 不允许 大于库存数量 
        let list = await execSql(`select C.*,G.flag,S.stoenum from lv_cart as C,lv_goods as G,lv_goods_sku as S where C.goodsid=G.id and C.skuid=S.id and C.uid='${uid}' or C.temp_key='${uid}'`)
        return list
    }

    static async getCartIdInfo(cartArr){
         let list = await execSql(`select C.*,G.flag,S.stoenum from lv_cart as C,lv_goods as G,lv_goods_sku as S where C.goodsid=G.id and C.skuid=S.id  and C.id in(${cartArr}) `)
        return list
    }
    /**
     * 删除购物车
     * @param {String} idArr 1,2,3=>[1,2,3].join()  
     */
    static async del(idArr){
        let res = await execSql(`delete from lv_cart where id in (${idArr}) or temp_key in (${idArr})`)
        return res
    }
   /**
    * 
    * @param {Number} goodsid  商品id
    * @param {Number} skuid skuid
    * @param {Number} id  购物车id
    * @param {Number} num  购物车数量
    * @returns 
    */
    static async eidt(goodsid,skuid,id,num){
        // 存在2种情况 1. 库存更新了  2.商品下架了 
        //商品上下架状态
        //查询商品表 看下 图片 同时检查下上下架状态
        let goods = await execSql(`select * from lv_goods where id=${goodsid}`)
        if(goods.length>0){
            //如果商品是上架状态说明允许购买
            if(goods[0].flag==1){
                //判断下库存 是否可以加入到购物车
                let sku = await execSql(`select * from lv_goods_sku where id=${skuid}`)
                if(sku.length>0){
                    //如果库存数量 大于 用户加入购物车的数量 说明允许购买
                    if(sku[0].stoenum>=num){
                       let res = await execSql(`update lv_cart set num=${num} where id='${id}' or temp_key='${id}'`)
                        return {type:"success",data:res}
                    }else{
                        return {type:"error",msg:"库存不足"}   
                    }
                     
                }else{
                     return {type:"error",msg:"商品属性不存在"}
                }
               
            }else{
                return {type:"error",msg:"商品已经下架"}
            }
            
        }else{
            //商品不存在
            return {type:"error",msg:"商品不存在"}
        }
        
    }
    /**
     * 添加购物车
     * @param {*} goodsid 商品id 
     * @param {*} num 数量
     * @param {*} uid  用户id
     * @param {*} skuid skuid
     * @param {*} type 类型 1酒店 2商品 3门票 4项目
     * @param {*} skuname sku组合名称
     * @param {*} temp_key 临时key
     * @returns 
     */
    static async add(goodsid,num,uid,skuid,skuname,temp_key=""){
        //查询商品表 看下 图片 同时检查下上下架状态
        let goods = await execSql(`select * from lv_goods where id=${goodsid}`)
        if(goods.length>0){
            //如果商品是上架状态说明允许购买
            if(goods[0].flag==1){
                //判断下库存 是否可以加入到购物车
                let sku = await execSql(`select * from lv_goods_sku where id=${skuid}`)
                if(sku.length>0){
                    //如果库存数量 大于 用户加入购物车的数量 说明允许购买
                    if(sku[0].stoenum>=num){
                        // 如果用户 曾经把这个数据据已经加入到购物车了 那么就直接让数量加加
                        let cartArr = await execSql(`select * from lv_cart where uid=${uid} or temp_key='${temp_key}' and goodsid=${goodsid} and skuid=${skuid}`)
                        let res=null
                        if(cartArr.length>0){
                            res = await execSql(`update lv_cart set num=num+${num} where uid=${uid} or temp_key='${temp_key}' and goodsid=${goodsid} and skuid=${skuid}`)
                        }else{
                             res =await execSql(`INSERT INTO lv_cart (goodsid, uid,goodsname, img, price, num, type, temp_key, skuid, skuname) VALUES ('${goodsid}', '${uid}','${goods[0].goodsname}', '${goods[0].img}', '${sku[0].price}', '${num}', '${goods[0].type}', '${temp_key}', '${skuid}', '${skuname}')`)
                        }
                       
                        return {type:"success",data:res}
                    }else{
                        return {type:"error",msg:"库存不足"}   
                    }
                     
                }else{
                     return {type:"error",msg:"商品属性不存在"}
                }
               
            }else{
                return {type:"error",msg:"商品已经下架"}
            }
            
        }else{
            //商品不存在
            return {type:"error",msg:"商品不存在"}
        }
        
    }


}
module.exports=cart